package com.zsy.controller;

import com.zsy.pojo.Electric_Power;
import com.zsy.pojo.RegionInfo;
import com.zsy.pojo.month;
import com.zsy.service.region_Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

@RestController
@RequestMapping("/region")
public class region_name_Controller {

    @Autowired
    private region_Service region_service;

    //查找所有城市的名字
    @GetMapping("/name")
    private List<RegionInfo> getName() {
        return region_service.getName();
    }

    //通过地区 子地区 时间来查找info
    @PostMapping("/SearchInfo")
    public List<Electric_Power> SearchInfo(String name, int loadName, LocalDate data) {
        return region_service.SearchInfo(name, loadName, data);
    }

    @GetMapping("/TotalRegion")
    public int TotalRegion() {
        return region_service.TotalRegion();
    }

    //查找总电量
    @PostMapping("/TotalElectric")
    public double TotalElectric(String name, int loadName, LocalDate data) {
        return region_service.TotalElectric(name, loadName, data);
    }

    @PostMapping("/AvgTemperature")
    public double AvgTemperature(String name, int loadName, LocalDate data) {
        return region_service.AvgTemperature(name, loadName, data);
    }

    @PostMapping("/SearchInfo_no_load_name")
    public double[] SearchInfo_no_load_name(String name, LocalDate data) {
        double[] doubles = new double[24];
        List<Electric_Power> list = region_service.SearchInfo_no_load_name(name, data);
        int k = 0;
        for (int i = 0; i < list.size(); i++) {
            Electric_Power electricPower = list.get(i);
            doubles[k] += electricPower.getPowerConsumption();
            k++;
            if (k == 20) {
                k = 0;
            }
        }
        k = 20;
        int flag = 0;
        for (int i = 0; i <= 19; i++) {
            flag++;
            if (flag > 5) {
                flag = 1;
                k++;
            }
            doubles[k] += doubles[i];
        }
        return doubles;
    }

    @GetMapping("/monthly_power_2023")
    public double[] monthly_power_2023() {
        List<month> list = region_service.monthly_power_2023();
        double[] array = new double[12];
        for (int i = 0; i < list.size(); i++) {
            array[i] = list.get(i).getTotalPowerConsumption();
        }
        return array;
    }

    @GetMapping("/monthly_power_2024")
    public double[] monthly_power_2024() {
        List<month> list = region_service.monthly_power_2024();
        double[] array = new double[12];
        for (int i = 0; i < list.size(); i++) {
            array[i] = list.get(i).getTotalPowerConsumption();
        }
        return array;
    }

    @PostMapping("/Forecast")
    public double[] Forecast(String name, int loadName, LocalDate data) {
        double[] doubles = new double[120];
        String s = data.toString();
        char[] array = s.toCharArray();

        array[3] = '4';
        s = new String(array);//2024
        data = LocalDate.parse(s);
//        System.out.println(data);
        for (int i = 0; i < 120; i++) {
            List<Electric_Power> list = region_service.SearchInfo(name, loadName, data);
            doubles[i] += list.get(i).getPowerConsumption();
        }

        array[3] = '3';
        s = new String(array);//2023
        data = LocalDate.parse(s);
//        System.out.println((data));
        for (int i = 0; i < 120; i++) {
            List<Electric_Power> list = region_service.SearchInfo(name, loadName, data);
            doubles[i] += list.get(i).getPowerConsumption();
            doubles[i] = doubles[i] / 2;
        }
        return doubles;
    }
}
